<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Help extends MY_Controller {

	public $left_menu_min;

	function __construct(){
		parent::__construct();
		$this->left_menu_min = array("menu" => "网校设置", "curr" => '/help', "list" => array("学校设置" => "/school", "学校资讯" => "/news", "帮助中心" => "/help", "友情连接" => "/friend_link", "网点管理" => "/wangdian", "问答管理" => "/question_topic", "客服设置" => "/kefu"));
		$this->smarty->assign("left_menu_min", $this->left_menu_min);
		$this->smarty->assign("menu", 'school');
	}

	function index($title = 0, $limit = 0){
		if($this->input->post()){
			$limit = 0;
			$post_data = $this->input->post();
			$title = $post_data['title'] ? urldecode($post_data['title']) : 0;
		}else{
			$title = urldecode($title);
		}
		$current_url = 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
		$conditions = array('title' => $title, 'school_id' => $this->user_data['school_id']);
		$this->load->model("Helps");
		$result = $this->Helps->get_help_list($limit, $this->offset, $conditions);
		$this->load->library('pagination');
		$page_config['base_url'] = '/help/index/' . $title;
		$page_config['total_rows'] = $result['count'];
		$page_config['per_page'] = $this->offset;
		$page_config['uri_segment'] = 4;
		$this->pagination->initialize($page_config);
		$pagination = $this->pagination->create_links();
		$this->smarty->assign("action", '帮助列表');
		$this->smarty->assign("title", $title);
		$this->smarty->assign("current_url", $current_url);
		$this->smarty->assign("item_list", $result['result']);
		$this->smarty->assign("pagination", $pagination);
		$this->smarty->display("help/list.html");
	}

	function add(){
		$post_data = $this->input->post();
		if($post_data){
			$post_data['add_time'] = time();
			$post_data['school_id'] = $this->user_data['school_id'];
			if(empty($post_data['status'])) $post_data['status'] = 0;
			if(!empty($post_data['img'])){
				$post_data['img'] = get_qiniu_url($post_data['img']);
			}
			if(isset($post_data['file'])) unset($post_data['file']);
			$this->db->insert("o2o_school_helps", $post_data);
			$insert_id = $this->db->insert_id();
			if($insert_id > 0){
				show_message($this->jump_url);
			}else{
				show_message($this->jump_url, "操作失败");
			}
		}else{
			$this->load->model("Helps");
			$conditions = array('school_id' => $this->user_data['school_id']);
			$category_list =  $this->Helps->get_help_category_list(0,0,$conditions);
			$this->smarty->assign("category_list", $category_list);
			$this->smarty->assign("action", '添加帮助');
			$this->smarty->display("help/save.html");
		}
	}

	function edit($id = 0){
		$post_data = $this->input->post();
		if($post_data){
			if(!empty($post_data['img'])){
				$post_data['img'] = get_qiniu_url($post_data['img']);
			}
			if(empty($post_data['status'])) $post_data['status'] = 0;
			if(isset($post_data['file'])) unset($post_data['file']);
			$this->db->where('id', $post_data['id']);
			$this->db->update('o2o_school_helps', $post_data);
			if($this->db->affected_rows() > 0){
				show_message($this->jump_url);
			}else{
				show_message($this->jump_url, "操作失败");
			}
		}else{
			if(intval($id) > 0){
				$help_info = get_field_by_where("o2o_school_helps", "*", "id='{$id}' AND school_id = '{$this->user_data['school_id']}'");
				if(!$help_info){
					show_message("/help", "数据不存在");exit;
				}
				$this->load->model("Helps");
				$conditions = array('school_id' => $this->user_data['school_id']);
				$category_list =  $this->Helps->get_help_category_list(0,0,$conditions);
				$this->smarty->assign("help_info", $help_info);
				$this->smarty->assign("category_list", $category_list);
				$this->smarty->assign("action", '编辑帮助');
				$this->smarty->display("help/save.html");
			}else{
				show_message("/help", "非法操作");
			}
		}
	}

	function delete($ids){
		if($_POST){
			$ids = $this->input->post('ids');
		}else{
			$ids = (array) $ids;
		}
		delete_data($ids, "o2o_school_helps");
		if($this->db->affected_rows() > 0){
			show_message($this->jump_url, "删除成功");
		}else{
			show_message($this->jump_url, "删除失败，请重试！");
		}
	}

	function category_list($name = 0, $limit = 0){
		if($this->input->post()){
			$limit = 0;
			$post_data = $this->input->post();
			$name = $post_data['name'] ? urldecode($post_data['name']) : 0;
		}else{
			$name = urldecode($name);
		}
		$current_url = 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
		$conditions = array('name' => $name, 'school_id' => $this->user_data['school_id']);
		$this->load->model("Helps");
		$result = $this->Helps->get_help_category_list($limit, $this->offset, $conditions);
		$this->load->library('pagination');
		$page_config['base_url'] = '/help/index/' . $name;
		$page_config['total_rows'] = $result['count'];
		$page_config['per_page'] = $this->offset;
		$page_config['uri_segment'] = 4;
		$this->pagination->initialize($page_config);
		$pagination = $this->pagination->create_links();
		$this->smarty->assign("action", '帮助列表');
		$this->smarty->assign("name", $name);
		$this->smarty->assign("current_url", $current_url);
		$this->smarty->assign("item_list", $result['result']);
		$this->smarty->assign("pagination", $pagination);
		$this->smarty->display("help/category_list.html");
	}

	function add_category(){
		$post_data = $this->input->post();
		if($post_data){
			$post_data['add_time'] = time();
			$post_data['school_id'] = $this->user_data['school_id'];
			if(empty($post_data['status'])) $post_data['status'] = 0;
			$this->db->insert("o2o_school_help_categorys", $post_data);
			$insert_id = $this->db->insert_id();
			if($insert_id > 0){
				show_message($this->jump_url);
			}else{
				show_message($this->jump_url, "操作失败");
			}
		}else{
			$this->smarty->assign("action", '添加帮助');
			$this->smarty->display("help/save_category.html");
		}
	}

	function edit_category($id = 0){
		$post_data = $this->input->post();
		if($post_data){
			if(empty($post_data['status'])) $post_data['status'] = 0;
			$this->db->where('id', $post_data['id']);
			$this->db->update('o2o_school_help_categorys', $post_data);
			if($this->db->affected_rows() > 0){
				show_message($this->jump_url);
			}else{
				show_message($this->jump_url, "操作失败");
			}
		}else{
			if(intval($id) > 0){
				$help_info = get_field_by_where("o2o_school_help_categorys", "*", "id='{$id}' AND school_id = '{$this->user_data['school_id']}'");
				if(!$help_info){
					show_message("/help/category_list", "数据不存在");exit;
				}
				$this->smarty->assign("help_info", $help_info);
				$this->smarty->assign("action", '编辑帮助');
				$this->smarty->display("help/save_category.html");
			}else{
				show_message("/help/category_list", "非法操作");
			}
		}
	}

	function delete_category($ids){
		if($_POST){
			$ids = $this->input->post('ids');
		}else{
			$ids = (array) $ids;
		}
		delete_data($ids, "o2o_school_help_categorys");
		if($this->db->affected_rows() > 0){
			show_message($this->jump_url, "删除成功");
		}else{
			show_message($this->jump_url, "删除失败，请重试！");
		}
	}
}